# import pandas as pd
# import matplotlib.pyplot as plt
# import seaborn as sns
#
# sns.set(style="white", font_scale=1.5)
# plt.rcParams['font.sans-serif'] = "Simsun"  # 支持中文字体
#
# # 定义文件路径
# file_path = "D:\文档\DLMU\HLJU实验结果1.xlsx"
#
# # 读取数据
# try:
#     data = pd.read_excel(file_path)
# except Exception as e:
#     print("读取 Excel 文件时出错:", e)
#     exit()
#
# # 检查必要的列是否存在
# required_columns = ["Num", "value1", "value2", "value3"]
# if not all(col in data.columns for col in required_columns):
#     print("数据中缺少必要的列：", required_columns)
#     exit()
#
# # 去除完全空的行
# data = data.dropna(how="all")
#
# # 初始化绘图
# plt.figure(figsize=(12, 8))
#
# # 绘制 value1 的折线图
# sns.lineplot(
#     data=data,
#     x="Num",  # x轴为 deptID
#     y="value1",  # y轴为 value1
#     label="MOPSO",
#     ci=None,
#     linewidth=2,  # 设置折线宽度
#     marker="^",
#     markersize=10,
#     # color='#e53528',
#     alpha=0.8,
# )
#
# # 绘制 value2 的折线图
# sns.lineplot(
#     data=data,
#     x="Num",  # x轴为 deptID
#     y="value2",  # y轴为 value2
#     label="NSGA-II",
#     ci=None,
#     linewidth=2,
#     marker="s",
#     markersize=10,
#     alpha=0.8,
# )
#
# # 绘制 value3 的折线图
# sns.lineplot(
#     data=data,
#     x="Num",  # x轴为 deptID
#     y="value3",  # y轴为 value3
#     label="MOGRO",
#     ci=None,
#     linewidth=2,
#     marker="o",
#     markersize=10,
#     alpha=0.8,
# )
#
# # 设置图形标题和标签
# # plt.title("MOLSO 和 MOGRO 的对比", fontsize=16)
# plt.xlabel("教学任务数", fontsize=14)
# plt.ylabel("目标函数值", fontsize=14)
# plt.xticks(data["Num"], fontsize=12)  # X轴刻度调整
# plt.yticks(fontsize=12)  # X轴刻度调整
# plt.xlim(39, 385)  # 将x轴范围从10到100
# plt.legend(title="方法", title_fontsize=12, fontsize=10, loc='upper left')
# plt.grid(False)
# plt.tight_layout()
#
# # 显示图形
# plt.show()


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

# sns.set(style="white")
# plt.rcParams['font.sans-serif'] = ["SimHei"]  # 支持中文字体

# 定义文件路径
file_path = "D:\文档\DLMU\HLJU实验结果1.xlsx"

# 读取数据
try:
    data = pd.read_excel(file_path)
except Exception as e:
    print("读取 Excel 文件时出错:", e)
    exit()

# 检查必要的列是否存在
required_columns = ["Num", "value1", "value2", "value3"]
if not all(col in data.columns for col in required_columns):
    print("数据中缺少必要的列：", required_columns)
    exit()

# 去除完全空的行
data = data.dropna(how="all")

# 初始化绘图
plt.figure(figsize=(12, 8))

# 数据
# x = np.arange(1, 6)  # X 轴数据点
scale_factor = 2
x = data["Num"]
y1 = data["value1"]
e1 = data["err1"] * scale_factor # 误差值
y2 = data["value2"]
e2 = data["err2"] * scale_factor # 误差值
y3 = data["value3"]
e3 = data["err3"] * scale_factor # 误差值

# 画折线图并添加误差线
plt.errorbar(x, y3, yerr=e3, fmt='-o', capsize=5, capthick=2, elinewidth=2, linewidth=3, color="#E60000",
             label="MOLSO")
plt.errorbar(x, y2, yerr=e2, fmt='-o', capsize=5, capthick=2, elinewidth=2, linewidth=3, color="#007FFF",
             label="NSGA-II")
plt.errorbar(x, y1, yerr=e1, fmt='-o', capsize=5, capthick=2, elinewidth=2, linewidth=3, color="#FFD700",
             label="MOPSO")
# 设置图形标题和标签
plt.xlabel("Number of teaching assignments", fontsize=16, fontname="SimHei", labelpad=10)
plt.ylabel("Total objective function value", fontsize=16, fontname="SimHei", labelpad=15)
xticks = np.linspace(30, 390, num=7)  # 6 个均匀间隔的刻度
plt.xticks(xticks.astype(int), fontsize=14, fontname="Times New Roman")  # 确保刻度为整数
plt.yticks(fontsize=14, fontname="Times New Roman")  # X轴刻度调整
# plt.xlim(data["Num"].min() - 9, data["Num"].max() + 5)  # 将x轴范围从10到100
plt.ylim(data[["value1", "value2", "value3"]].min().min() - 150,
         data[["value1", "value2", "value3"]].max().max() + 450)  # 将x轴范围从530到7800
plt.legend(markerscale=1, loc='lower right', prop={'size': 14, "family": "Times New Roman"})

# 设置刻度线样式
plt.tick_params(axis='both', which='both', length=6, width=1, colors='black')  # 设置刻度线的样式

plt.grid(False)
plt.tight_layout()

# 显示图形
plt.show()
